<template>
  <div class="wrapper">
    <div style="margin:70px auto ;color: #606266; font-size: 50px;text-align: center;font-family: '华文琥珀',serif">学生证书管理系统</div>
    <div style="margin:70px auto ; background-color: aliceblue;width: 350px; height: 390px;padding:20px;border-radius: 5px">
      <div style="margin: 10px 0; color: #606266; text-align: center;font-size: 30px"><b>登 录</b></div>
      <el-form :model="user" :rules="rules" ref="userForm">
        <el-form-item prop="username" style="margin: 30px 0">
          <el-input size="medium" @keyup.enter.native="login"  placeholder="用户名" prefix-icon="el-icon-user" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="password" style="margin: 30px 0">
          <el-input size="medium" @keyup.enter.native="login"  placeholder="密码" prefix-icon="el-icon-lock" show-password
                    v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row :span="24">
            <el-col :span="12">
              <el-input size="medium" @keyup.enter.native="login"  placeholder="验证码" prefix-icon="el-icon-key"
                        v-model="code"></el-input>
            </el-col>
            <el-col :span="12">
              <div @click="refreshCode" style="cursor: pointer;text-align: center; ">
                <PIdentity :identifyCode="identifyCode" style=""></PIdentity>
              </div>
            </el-col>
          </el-row>

        </el-form-item>
        <el-form-item style="margin: 10px 0; text-align: center">
          <el-button type="primary" size="medium" style="width: 100%" autocomplete="off" @click="login">登 录
          </el-button>

        </el-form-item>
        <el-form-item style="margin: 10px 0; text-align: center">
          <el-button type="warning"  size="medium" style="width: 100%"  autocomplete="off" @click="$router.push('/register')">注 册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {setRoutes} from "@/router";
import PIdentity from "@/components/PIdentity";
import {ElMessage} from "element-ui";

export default {
  name: "Login",
  components:{
    PIdentity,
  },
  data() {
    return {
      //验证码
      identifyCode:'',
      identifyCodes:'0123456789',
      code:'',

      user: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
      }
    }
  },
  mounted() {
    this.refreshCode();
  },
  methods: {
    login() {
      if(this.identifyCode!== this.code){
        this.code='';
        this.refreshCode();
        this.$message.error("验证码错误")
        return;
      }
      this.$refs['userForm'].validate((valid) => {
        if (valid) { //表单校验合法
          this.request.post("/user/login", this.user).then(res => {
            if (res.code==='200') {
              console.log(res.data)
              localStorage.setItem("user",JSON.stringify(res.data))//存储用户信息到浏览器
              localStorage.setItem("menus",JSON.stringify(res.data.menus))//存储用户信息到浏览器
              // 设置当前用户的路由
              setRoutes()
              if(res.data.role==='ROLE_STUDENT'){
                // this.$router.push("/front/home")
                this.$router.push("/")
              }else{
                this.$router.push("/")
              }

              this.$message.success("登录成功")
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      });
    },
    refreshCode() {
      this.identifyCode = '';
      this.makeCode(this.identifyCodes, 4);
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[
            this.randomNum(0, this.identifyCodes.length)]
      }
      console.log(this.identifyCode);
    },
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },

  }
}

</script>

<style scoped>
.wrapper {
  height: 100vh;
  //background-image: linear-gradient(to bottom right, #FC4668, #3F5EF8);
  overflow: hidden;
  background-color:rgba(200,224,228,0.8);
}
</style>








